package controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import dao.BookDao;
import domin.Book;
import domin.Bookloss;
import lombok.extern.slf4j.Slf4j;
import service.BooklossService;
import service.Bookservice;
import util.Condition;
import util.JSONUtil;
import util.Pagination;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;

@Slf4j
@WebServlet("/bookloss.ctl")
public class BooklossController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        String id_str = request.getParameter("id");
        try {
            if(id_str!=null){
                int id = Integer.parseInt(id_str);
                responseBookloss(id, response);
                log.trace("查找id" + id);
            }else {
                this.responseBooklosses(response, null, null);
            }
        } catch (SQLException e){
            log.error("数据库异常"+ e.getMessage());
        } catch(Exception e){
            log.error("message","网络异常" + e.getMessage());
        }
    }

    private void responseBookloss(int id, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        Bookloss bookloss = BooklossService.getInstance().find(id);
        String bookloss_json = JSON.toJSONString(bookloss);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data",bookloss_json);
        response.getWriter().println(jsonObject);
    }
    private void responseBooklosses(HttpServletResponse response, Collection<Condition> condition, Pagination pagination)
            throws ServletException, IOException, SQLException {
        Collection<Bookloss> booklosses = BooklossService.getInstance().findAll(condition,pagination);
        String booklosss_json = JSON.toJSONString(booklosses, SerializerFeature.DisableCircularReferenceDetect);
        response.getWriter().println(booklosss_json);
    }
    @Override
    protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id_str = request.getParameter("id");
        int id = Integer.parseInt(id_str);
        response.setContentType("application/json;charset=UTF-8");
        JSONObject message = new JSONObject();
        try {
            BooklossService.getInstance().delete(id);
            message.put("message", "删除成功");
        }catch (SQLException e){
            message.put("message", "数据库操作异常");
        }catch(Exception e){
            message.put("message", "网络异常");
        }

        response.getWriter().println(message);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        request.setCharacterEncoding("UTF-8");
        String bookloss_json = JSONUtil.getJSON(request);
        Bookloss bookloss = JSON.parseObject(bookloss_json, Bookloss.class);
        response.setContentType("application/json;charset=UTF-8");
        JSONObject message = new JSONObject();
        try {
            BooklossService.getInstance().add(bookloss);
            message.put("message", "增加成功");
        } catch (SQLException e) {
            message.put("message", "数据库操作异常");

            log.error(e.getMessage(),e);
        } catch (Exception e) {
            message.put("message", "网络异常");
            log.error(e.getMessage(),e);
        }
        response.getWriter().println(message);
    }
    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String bookloss_json = JSONUtil.getJSON(req);
        Bookloss bookloss = JSON.parseObject(bookloss_json, Bookloss.class);
        resp.setContentType("application/json;charset=UTF-8");
        JSONObject message = new JSONObject();
        try {

            BooklossService.getInstance().update(bookloss);
            message.put("message","修改成功");
        }catch (SQLException e){

            message.put("message","数据库异常"+e.getMessage());

            log.error("数据库异常" + e.getMessage());

        }catch (Exception e){
            message.put("message","网络异常"+e.getMessage());

            log.error("网络异常");
        }finally {
            resp.getWriter().println(message);
        }
    }

}
